package com.saiaweather.android.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.util.ArrayList;
import java.util.List;

public class SaiaWeatherDB {
    public static final String Db_NAME = "saia_weather";
    public static final int VERSION = 1;
    public static SaiaWeatherDB saiaWeatherDB;
    public SQLiteDatabase db;

    //实例化对象函数
    private SaiaWeatherDB(Context context) {
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(context, Db_NAME, null, VERSION);
        db = dbHelper.getWritableDatabase();
    }

    //获取实例化对象
    public synchronized static SaiaWeatherDB getInstance(Context context) {
        if (saiaWeatherDB == null) {
            saiaWeatherDB = new SaiaWeatherDB(context);
        }
        return saiaWeatherDB;
    }
    //储存Province到db

    public void saveProvince(Province province) {
        if (province != null) {
            ContentValues values = new ContentValues();
            values.put("provinceName", province.getProvinceName());
            values.put("provinceCode", province.getProvinceCode());
            db.insert("Province", null, values);
        }
    }
    /*
    private  int id;
    private  String provinceName;
    private  int provinceCode;
     */

    //读取Province数据

    @SuppressLint("Range")
    public  List<Province> loadProvince() {
        List<Province> list = new ArrayList<>();
        Cursor cursor = db.query("Province", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                Province province = new Province();
                province.setId(cursor.getInt(cursor.getColumnIndex("id")));
                province.setProvinceName(cursor.getString(cursor.getColumnIndex("provinceName")));
                province.setProvinceCode(cursor.getString(cursor.getColumnIndex("provinceCode")));
                list.add(province);
            } while (cursor.moveToNext());
        }
        //????/
        if(cursor!=null) {
            cursor.close();
        }



        return list;
    }

    //-----------------------
    //
    public void saveCity(City city) {
        if (city != null) {
            ContentValues values = new ContentValues();
            values.put("cityName", city.getCityName());
            values.put("cityCode", city.getCityCode());
            values.put("provinceId", city.getProvinceId());
            db.insert("City", null, values);
        }
    }
    /*
    private int id;
    private String cityName;
    private int cityCode;
    private int provinceId;
     */

    //
    @SuppressLint("Range")
    public List<City> loadCity(int provinceId) {
        List<City> list = new ArrayList<>();
        Cursor cursor = db.query("City", null,
                "provinceId = ? ", new String[] {String.valueOf(provinceId)},
                null, null, null);
        if (cursor.moveToFirst()) {
            do {
                City city = new City();
                city.setId(cursor.getInt(cursor.getColumnIndex("id")));
                city.setCityName(cursor.getString(cursor.getColumnIndex("cityName")));
                city.setCityCode(cursor.getString(cursor.getColumnIndex("cityCode")));
                city.setProvinceId(cursor.getInt(cursor.getColumnIndex("provinceId")));
                list.add(city);
            } while (cursor.moveToNext());
        }
        if(cursor != null) {
            cursor.close();
        }

        return list;
    }
    //-------------------------------------
    //

    public void saveCounty(County county) {
        if (county != null) {
            ContentValues values = new ContentValues();
            values.put("countyName", county.getCountyName());
            values.put("weatherId", county.getWeatherId());
            values.put("cityId", county.getCityId());
            db.insert("County", null, values);
        }
    }
    //
    @SuppressLint("Range")
    public List<County> loadCounty(int cityId) {
        List<County> list = new ArrayList<>();
        Cursor cursor = db.query("County", null, "cityId = ? ", new String[] {String.valueOf(cityId)}, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                County county = new County();
                county.setId(cursor.getInt(cursor.getColumnIndex("id")));
                county.setCountyName(cursor.getString(cursor.getColumnIndex("countyName")));
                county.setWeatherId(cursor.getString(cursor.getColumnIndex("weatherId")));
                county.setCityId(cursor.getInt(cursor.getColumnIndex("cityId")));
                list.add(county);
            } while (cursor.moveToNext());
        }

            cursor.close();

        return list;
    }


}
